﻿using HospitalFollowUpSystem.Utils;
using MetroFramework.Forms;
using System;
using System.Windows.Forms;

namespace HospitalFollowUpSystem
{
    public partial class FrmLogin : MetroForm
    {
        /// <summary>
        /// 操作错误错误 提示框 标题头
        /// </summary>
        private const String OperateErrStr = "操作错误";

        public FrmLogin()
        {
            InitializeComponent();
            txtUserＮame.Focus();
        }

        //窗口加载事件
        private void FrmLogin_Load(object sender, EventArgs e)
        {
        }

        //登录按钮-点击事件
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            String passWord = txtPassＷord.Text.Trim();
            String userName = txtUserＮame.Text.Trim();

            //密码or账号空
            var DialogResultValue = DialogResult.None;
            if (userName.Equals("") || passWord.Equals(""))
            {
                MessageBox.Show("账号和密码不可能为空", OperateErrStr);
            }
            else if (LoginCheck(userName, passWord, out DialogResultValue))
            {
                GlobalVar.OperName = userName;
                this.DialogResult = DialogResultValue;
                this.Close();
            }
            else
            {
                MessageBox.Show("账号/密码组合错误", "口令错误");
            }
        }

        // 用户名框-回车往下跳
        private void TxtUserＮame_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                txtPassＷord.Focus();
                txtPassＷord.SelectAll();
            }
        }

        // 密码框-回车往下跳
        private void TxtPassＷord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                BtnLogin.Focus();
            }
        }


















        /// <summary>
        /// 登录检查
        /// </summary>
        /// <param name="un">密码</param>
        /// <param name="pwd">账号</param>
        /// <param name="DialogResultValue"> OK 是设置界面 NO 是主界面 None 登录错误</param>
        /// <returns></returns>
        private Boolean LoginCheck(String un, String pwd, out DialogResult DialogResultValue)
        {
            if (un.Equals("sa") && pwd.Equals("123"))
            {
                DialogResultValue = DialogResult.OK;
                return true;
            }
            else
            {
                //TASK 需要加入数据库验证账号密码
                if (un.Equals("admin") && pwd.Equals("123"))
                {
                    DialogResultValue = DialogResult.No;
                    return true;
                }
                else
                {
                    DialogResultValue = DialogResult.None;
                    return false;
                }
            }
        }
    }
}
